//var global declaration
(function ($) {
	 var itemsTemplate = new Template(['<tr>',
                                       '<td>#{itemId}</td><td>#{itemnamew}</td><td>#{itemCode}</td><td><a onclick="editItemData(#{itemId},\'#{itemnamew}\',\'#{itemCode}\');">Edit</a> <a onclick="deleteItemData(#{itemId});">Delete</a></td>',
	                                  '</td>',
	                                  '</tr>'].join(''));
	                                  
	 var searchItems = new Template(['<tr>',
                                    '<td>#{itemId}</td><td>#{itemnamew}</td><td>#{itemCode}</td>',
                                  '</td>',
                                  '</tr>'].join(''));
      
   
      //Show Content
      showCategories = function(showId, hideId) {
     	 $('#'+showId).removeClass('hide');
     	 $('#'+hideId).addClass('hide');
     	 if(showId == 'managePostage'){
     		 $('#manageContent').removeClass('hide');
     		 $('#inventoryContent').addClass('hide');
     	 } else if(showId == 'invetory') {
     		 $('#inventoryContent').removeClass('hide');
     		 $('#manageContent').addClass('hide');
     		 $("#generateLabels").addClass('hide');
     		 $(".addItem").removeClass('hide');
     		 $('#itemName').val('');
      	     $('#itemCode').val('');
      	    $('#searchItemtxt').val('');
     	 }
       };
      showCategories('managePostage','invetory');

      //Upload
      uploadFiles = function() {
    	  var pathvalue = $('#uploadFile').val();
    	  var excelfile=new FormData();
    	  excelfile.append('excel',$("#uploadFile").prop("files")[0]);
    
    	  $.ajax({
    			url: "uploadFile",
    		    type: 'POST',
    		    data: excelfile,
    		    cache: false,
    		    processData: false, //Don't process the files
    		    contentType: false, // Set content type to false as jQuery will tell the server its a query string request
    		    success: function(data)
    		    {
    		    	$("#uploadstatus").html($("#uploadFile").val()+" "+data);
    		    	$("#uploadFile").val('');
    		    }
    		    		
    		});
      };
      
      generatePostageLabel=function(file){ 	  
    	  
    	  $.ajax({
    		  url:"generateLabel",
    		  type:'POST',
    	      data:"file="+file,
    	      success:function(data){
    	    	  alert(data);
    	      }
    	  });
    	  }
      
      //Additem
      addItem = function() {
    	  var ItemDataTemp={},obj;
    	  ItemDataTemp.itemName = $('#itemName').val();
    	  ItemDataTemp.itemCode = $('#itemCode').val();
    	  obj = Object.toJSON(ItemDataTemp);
    	  $("#errorMsg").addClass('hide');
    	  if($('#itemId').val() == null || $('#itemId').val() == "") {
    		  $('#itemId').val(0);
    	  }
    	  $s.addItemDataDetails($('#itemName').val(),$('#itemCode').val(),$('#itemId').val(),{
    		  success: function(data){
	    			  if (data != null) {
	    				  $("#errorMsg").html('Added item successfully.').removeClass('hide');
	    				  if($("#itemBtn").html() == "Update"){
	    					  $(".addItem,.editItem,.searchItem").addClass('hide');
	    					  $(".editItem").removeClass('hide');
	    					  getItemDataList();
	    					  $("#itemBtn").html('Add Item');
	    				  }
	    			  } 
    			  }
    	  });
      };
      addItems = function() {
    	  $s.addItemFromFile({
    		  success: function(data){
    		  }
    	  });
      };
      //Add item list
      getItemDataList = function() {
    	  $("#reportResultsBody").html('');
    	  $s.itemDataList({
    		  success: function(data){
    			  var itemTempObj = {};
	    			  if (Boolean(data) && data.length > 0) {
	    				  for (var i = 0; i < data.length; i++) {
	    					  itemTempObj = data[i];
	    					  $("#reportResultsBody").append(itemsTemplate.evaluate(itemTempObj));
	    				  }
	
	    			  } else {
	    				  $("#reportResultsBody").html('<tr><td colspan="4" class="red">No items available</td></tr>');
	    			  }    		  
    			  }

    	  });
      };

      shomMenuContent = function(showId) {
    	  $(".addItem,.editItem,.searchItem,#manageContent,#generateLabels").addClass('hide');
    	  if(showId == 'addItem'){
    		  $("#errorMsg").html('').removeClass('hide');
    		  $(".addItem").removeClass('hide');
    		  $('#itemName').val('');
        	  $('#itemCode').val('');
    		  if($("#itemBtn").html() == "Update"){
				  $("#itemBtn").html('Add Item');
			  }
    	  } else if(showId == 'editItem'){
    		  getItemDataList();
    		  $(".editItem").removeClass('hide');
    		  $('#itemId').val('');
    	  } else if(showId == 'searchItem'){
    		  $(".searchItem").removeClass('hide');
    		  $('#searchItemtxt').val('');
    	  }
    	  else if(showId=='upload'){
    		  $("#generateLabels").addClass('hide');
    		  $("#manageContent").removeClass('hide');
    	  }
    	  else if(showId=='generateLabels'){
    		  $("#manageContent").addClass('hide');
    		  $("#uploadstatus").html('');
    		  $.ajax({
    			  url: "showUploadedFiles",
    			  success:function(data){
    				  $("#generateLabels").html(data);
    			  },
    		  error:function(data){
    			  $("#generateLabels").html("Something unexpected Occurs");
    		  }
    			  
    			    			});
    		  
    		  
    		  $("#generateLabels").removeClass('hide');
    	  }
      };
      
      editItemData = function(itemId,ItemName,ItemCode) {
    	  $(".addItem,.editItem,.searchItem").addClass('hide');
    	   $('#itemName').val(ItemName);
    	   $('#itemCode').val(ItemCode);
    	   $(".addItem").removeClass('hide');
    	   $('#itemId').val(itemId);
    	   $("#itemBtn").html('Update');
    	   
      }
      
      deleteItemData = function(itemId) {
    	   $(".addItem,.editItem,.searchItem").addClass('hide');
    	   $(".editItem").removeClass('hide');
    	   $s.deleteItem(itemId,{
     		  success: function(data){
				   getItemDataList();
     			  }
     	  });
      }
    //Search items
      searchItemsData = function() {
    	  $("#searchBody").html('');
    	  if ($('#searchItemtxt').val() != null && $('#searchItemtxt').val() != "") {
    		  $s.searchItemsData($('#searchItemtxt').val(),{
    			  success: function(data){
    				  var itemTempObj = {};
	    			  if (Boolean(data) && data.length > 0) {
	    				  for (var i = 0; i < data.length; i++) {
	    					  itemTempObj = data[i];
	    					  $("#searchBody").append(searchItems.evaluate(itemTempObj));
	    				  }
	
	    			  } else {
	    				  $("#searchBody").html('<tr><td colspan="4" class="red">No items available</td></tr>');
	    			  }
    			  }
    		  });

    	  } else {
    		 // $("#searchBody").html('<tr><td colspan="4" class="red">No items available</td></tr>');
    	  }
     }
      
      
      
})(jQuery);

